package xiangya.bysj.client.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import xiangya.bysj.entity.Medic;
import xiangya.bysj.service.impl.MedicServiceImpl;
import xiangya.bysj.util.JsonUtil;
import xiangya.bysj.util.StringUtil;

/**
 * 药品controller
 * 
 * @author Mr.zhang
 *
 */
@Controller
@RequestMapping(value = "/client/medic", produces = "application/json;charset=UTF-8")
public class MedicClientController {

	@Resource(name = "medicService")
	private MedicServiceImpl medicService;

	@RequestMapping("/queryByName")
	@ResponseBody
	public String query(String medicName) {
		String result = StringUtil.RES_FAIL;
		try {
			if (medicName != null && !medicName.equals("")) {
				List<Medic> list = medicService.getByName(medicName);
				if (list != null && list.size() > 0) {
					result = JsonUtil.getJson(list);
				}
			}
		} catch (Exception e) {
			result = StringUtil.RES_FAIL;
		}
		//result = callback + "(" + result + ")";
		return result;
	}

	@RequestMapping("/queryById")
	@ResponseBody
	public String query(int medicId) {
		String result = StringUtil.RES_FAIL;
		try {
			if (medicId > 0) {
				Medic medic = medicService.getById(medicId);
				if (medic != null) {
					result = JsonUtil.getJson(medic);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = StringUtil.RES_FAIL;
		}
		return result;
	}
}
